package com.caizi.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class MakeJavaResourceList {

    // private String resourcePath = null;
    // private String destSheet = null;

    private List<String[]> bufferedContent = new ArrayList<String[]>();

    // public MakeJavaResourceList(String resourcePath, String destSheet) {
    // this.resourcePath = resourcePath;
    // this.destSheet = destSheet;
    // }

    public void makeList(String resourcePath, String destSheet) throws Exception {
        makeListRecursion(resourcePath, destSheet);
        writeSheet(new File(destSheet));
    }

    private void makeListRecursion(String resourcePath, String destSheet) throws Exception {
        File resource = new File(resourcePath);
        if (resource.exists()) {
            if (resource.isDirectory()) {
                File[] listFiles = resource.listFiles(new FileFilter() {
                    public boolean accept(File pathname) {
                        if (".svn".equalsIgnoreCase(pathname.getName()) || "bin".equals(pathname.getName())
                                || "test".equalsIgnoreCase(pathname.getName())) {
                            return false;
                        }
                        return true;
                    }
                });
                for (File file : listFiles) {
                    makeListRecursion(file.getAbsolutePath(), destSheet);
                }
            } else {
                if (resource.getName().endsWith(".java")) {
                    append(resource);
                }
            }
        }

    }

    private void writeSheet(File destSheet) throws IOException, Exception {
        WritableWorkbook wwb = null;
        Workbook wb = null;
        try {
            wb = Workbook.getWorkbook(destSheet);
            wwb = Workbook.createWorkbook(new File(destSheet.getParent() + File.separator + "开发源程序清单_代收付项目_V1.0.xls"), wb);
            WritableSheet ws = wwb.getSheet("程序清单");
            for (int i = 0; i < bufferedContent.size(); i++) {
                String[] detail = bufferedContent.get(i);
                Label label = new Label(0, i + 2, detail[0]);
                ws.addCell(label);
                label = new Label(1, i + 2, detail[1]);
                ws.addCell(label);
                System.out.println("已写入一条！" + detail[0] + "~" + detail[1]);
            }
            wwb.write();
        } catch (Exception e) {
            throw e;
        } finally {
            wb.close();
            wwb.close();
        }
    }

    private void append(File resource) throws IOException {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(resource));
            while (br.ready()) {
                String line = br.readLine();
                if (null != line && line.contains("*") && line.contains("Description:")) {
                    String description = line.substring(line.indexOf("Description:") + "Description:".length());
                    if (null == description || "".equals(description)) {
                        break;
                    }
                    if (description.endsWith("<br>")) {
                        description = description.substring(0, description.indexOf("<br>"));
                    }
                    String src = resource.getAbsolutePath();
                    bufferedContent.add(new String[] { description.trim(), src });
                    break;
                }
            }
        } catch (IOException e) {
            throw e;
        } finally {
            br.close();
        }
    }

    public static void main(String... strings) throws Exception {
        final String RESOURCE_PATH = "F:/workspace/bosent_baseline_application_agent2.2";
        final String DEST_PATH = "E:/开发源程序清单_收费项目_V1.0.xls";
        new MakeJavaResourceList().makeList(RESOURCE_PATH, DEST_PATH);
    }
}
